package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Elder;
import com.zzyl.entity.Reservation;
import com.zzyl.entity.TimeCount;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ElderMapper;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.MobThreadLocal;
import com.zzyl.vo.ReservationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private ElderMapper elderMapper;

    /**
     * 分页查找预约
     *
     * @param page   页码
     * @param size   每页大小
     * @param name   预约人姓名
     * @param phone  预约人手机号
     * @param status 预约状态
     * @param type   预约类型
     * @return 预约列表
     */
    @Override
    public PageResponse<ReservationVo> findByPage(int page, int size, String name, String phone, Integer status, Integer type, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(page, size);
        Long userId = MobThreadLocal.getUserId();
        Page<Reservation> byPage = reservationMapper.findByPage(page, size, name, phone, status, type, userId, startTime, endTime);
        return PageResponse.of(byPage, ReservationVo.class);
    }

    @Override
    public Integer getCancelledCount() {
        Long createBy = MobThreadLocal.getUserId();
        LocalDateTime begin = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime end = LocalDateTimeUtil.endOfDay(LocalDateTime.now());
        return reservationMapper.getCancelledCount(createBy, begin, end);
    }

    @Override
    public List<TimeCount> countByTime(long time) {
        LocalDateTime date = LocalDateTimeUtil.of(time);
        LocalDateTime begin = LocalDateTimeUtil.beginOfDay(date);
        LocalDateTime end = LocalDateTimeUtil.endOfDay(date);
        return reservationMapper.countByTime(begin, end);

    }

    @Override
    public void addReservation(ReservationDto reservationDto) {
        String mobile = reservationDto.getMobile();
        LocalDateTime begin = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime end = LocalDateTimeUtil.endOfDay(LocalDateTime.now());
        List<Reservation> list = reservationMapper.getReservationByMobile(mobile, begin, end);
        long count1 = list.stream().filter(reservation -> reservation.getStatus() == 2).count();
        if (count1 >= 3) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        long count2 = list.stream().filter(reservation -> reservation.getStatus() == 0 || reservation.getStatus() == 1).count();
        if (count2 > 0) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        if(reservationDto.getType() == 1){
            Elder elder = elderMapper.selectByPrimaryKey(reservationDto.getElderId());
            if (elder == null) {
                throw new BaseException(BasicEnum.ELDER_NOT_EXIST);
            }
            if (elder.getStatus() == 5) {
                throw new BaseException(BasicEnum.CANNOT_RESERVATION_DUE_ELDER_ALREADY_RETREATED);
            }
        }
        Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        reservationMapper.addReservation(reservation);
    }

    @Override
    public PageResponse<ReservationVo> findReservationPage(int pageNum, int pageSize, String status) {
        PageHelper.startPage(pageNum, pageSize);
        Long createBy = MobThreadLocal.getUserId();
        Page<ReservationVo> list = reservationMapper.findReservationPage(status, createBy);
        return PageResponse.of(list, ReservationVo.class);
    }

    @Override
    public void cancelReservation(Long id) {
        reservationMapper.cancelReservation(id);
    }

}

